var _animation; // 动画实体
Page({
  data: {
    auxiliary: { "current": 0, "showFunction": false, "play": false, "likeDate": [], "yinfuAnimationCount": [{ "top": 0 }, { "top": 0 }], "showBasePop": false, "showIssue": true, " showAnswerYes": false, "showAnswerNo": false,"answerText":""},
    journeyData: { "videoList": [
      { "ip": "https://image.xingqubaike.com:8443", "url": "/video/diary/2019/08/17/dd37b4c7-261a-4f71-9754-4964978a2d14.mp4", "title": "这是小李拍的一个精彩视频", "userName": "A____丶Lin沣", "userIp": "", "userUrl": "https://image.xingqubaike.com:8443/minicard/upload/avatar/cb8bd659-f6e0-4d55-b921-81a060865677.png", "like": "321W+", "comment": "9999", "share": "2462", "issue": { "title": "视频中题目正确答案是几号选择视频中题目正确答案是几号选择哈哈", "option": ["屈打成招屈打成招哈哈", "卧薪尝胆卧薪尝胆哈哈"], "answer": 1, 'addr': "天利中央花园", "longitude": 0,"latitude":0 }},
      { "ip": "https://image.xingqubaike.com:8443", "url": "/video/diary/2019/07/11/9d31710f-a53e-4f1c-8f42-d62acdce1d02.mp4", "title": "然后小明也拍了一个精彩视频", "userName": "小强同学", "userIp": "", "userUrl": "https://image.xingqubaike.com:8443/minicard/upload/avatar/501bf992-4183-439a-b20b-ad69915fe6c6.png", "like": "2545", "comment": "586", "share": "258" ,"issue": { "title": "大家猜猜这是哪里啊？？？", "option": ["中国", "新加坡"], "answer": 0 }},
      { "ip": "https://image.xingqubaike.com:8443", "url": "/video/diary/2019/08/12/9d585532-10a3-4310-91fa-3a7fe59bddec.mp4", "title": "接着老王也拍了一个还不错的视频", "userName": "隔壁老王", "userIp": "", "userUrl": "https://image.xingqubaike.com:8443/minicard/upload/interest/2019/08/11/060b156a-4cc9-420d-a6c7-6d0ef476e7df.png", "like": "0", "comment": "0", "share": "0" }]}
  },
  onLoad: function (options) {
    var _this = this
    var index = _this.data.auxiliary.current
    var video = wx.createVideoContext('myVideo' + index)
    video.play()
    _this.setData({
      ["auxiliary.play"]: true,
    })
    _this.issueOut = setTimeout(function () {
      if (_this.data.journeyData.videoList[index].issue != null && _this.data.journeyData.videoList[index].issue != '') {
        _this.setData({
          ["auxiliary.showBasePop"]: true,
        })
      } else {
        _this.setData({
          ["auxiliary.showBasePop"]: false,
        })
      }
    }, 3000)
  },
  onReady:function(){
    var _this = this
  },
  onShow:function(){
    var _this = this
    //_this.alternate();
  },
  /**
   * 滑动视频
   */
  selectCurrent: function (e) {
    var _this = this
    clearTimeout(_this.issueOut);
    _this.setData({
      ["auxiliary.showBasePop"]: false,
    })
    _this.closeMC();
    var newIndex = e.detail.current
    var oldIndex = _this.data.auxiliary.current
    var video = wx.createVideoContext('myVideo' + oldIndex)
    video.pause()
    var video = wx.createVideoContext('myVideo' + newIndex)
    video.play()
    _this.setData({
      ["auxiliary.current"]: newIndex,
      ["auxiliary.play"]: true,
      ["auxiliary.likeDate"]: [],
    })
    _this.issueOut = setTimeout(function () {
      if (_this.data.journeyData.videoList[newIndex].issue != null && _this.data.journeyData.videoList[newIndex].issue != '') {
        _this.setData({
          ["auxiliary.showBasePop"]: true,
        })
      }
    }, 3000)
  },
  /**
   * 视频点击事件
   */
  videoClick:function(e){
    var _this = this
    var newTime = e.timeStamp
    var oldTime = e.currentTarget.dataset.time
    console.log(e)
    if (newTime - oldTime >0){
      if (newTime - oldTime < 400){
        clearTimeout(_this.lastTapOut);
        var x = e.detail.x;
        var y = e.detail.y;
        var arr = _this.data.auxiliary.likeDate;
        var data = { "animationX": x, "animationY": y,"showLike":true}
        arr.push(data)
        _this.setData({
          ["auxiliary.likeDate"]: arr,
        })
        _this.biubiubiu(parseInt(_this.data.auxiliary.likeDate.length - 1));
      }else{
        _this.lastTapOut = setTimeout(function () {
          var play = _this.data.auxiliary.play
          var oldIndex = _this.data.auxiliary.current
          var video = wx.createVideoContext('myVideo' + oldIndex)
          if (play){
            video.pause()
            _this.setData({
              ["auxiliary.play"]: false,
            })
          }
        }, 300);
      }
    }
    this.setData({
      lastClickTime: newTime
    })
  },
  /**
   * 播放视频
   */
  playVideo: function(){
    var _this = this
    var oldIndex = _this.data.auxiliary.current
    var video = wx.createVideoContext('myVideo' + oldIndex)
    video.play()
    _this.setData({
      ["auxiliary.play"]: true,
    })
  },
  /**
   * 打开功能面板
   */
  openFunction: function(){
    var _this = this
    _this.setData({
      ["auxiliary.showFunction"]: _this.data.auxiliary.showFunction?false:true,
    })  
  },
  /**
   * 点赞动画
   */
  biubiubiu:function(index){
    console.log(index)
    var _this = this
    _animation = wx.createAnimation({
      duration: 1500,
      timingFunction: 'ease',
    })
    _animation.scale(3).rotate((index % 2 == 0 ? "60" : "-60")).opacity(0).step()
    _this.setData({
      ["auxiliary.likeDate[" + index + "].animation"]: _animation,
    })
    setTimeout(function () {
      _this.setData({
        ["auxiliary.likeDate[" + index + "].showLike"]: false,
      })
    }, 1500)
  },
  /**
   * 动画的交替
   */
  alternate: function(){
    var _this = this
    _this.yinfuAniOut = setTimeout(function () {
      var arr = _this.data.auxiliary.yinfuAnimationCount
      if (arr[0].top>=60){
        arr.splice(0, 1);
        _this.setData({
          ["auxiliary.yinfuAnimationCount"]: arr,
        })
        var data = {"top": 0}
        arr.push(data);
      }else{
        arr[0].top++;
        _this.setData({
          ["auxiliary.yinfuAnimationCount"]: arr,
        })
      }
      _this.alternate();
    }, 20)
  },
  //销毁定时器
  onHide: function () {
    var _this = this
    clearTimeout(_this.yinfuAniOut);
  },
  onUnload: function () {
    var _this = this
    clearTimeout(_this.yinfuAniOut);
  },
  /**
   * 返回上一页
   */
  toBack:function(){
    wx:wx.navigateBack({
      delta: 1,
    })
  },
  /**
   * 选中答案
   */
  selectIssueOption:function(e){
    var _this = this;
    var index = e.currentTarget.dataset.index;
    var current = _this.data.auxiliary.current
    var answer = _this.data.journeyData.videoList[current].issue.answer
    _this.setData({
      ["auxiliary.showIssue"]: false,
    })
    if (answer == index){
      _this.setData({
        ["auxiliary.showAnswerYes"]: true,
      })
    }else{
      var answerText = _this.data.journeyData.videoList[current].issue.option[answer]
      _this.setData({
        ["auxiliary.showAnswerNo"]: true,
        ["auxiliary.answerText"]: answerText,
      })
    }
  },
  /**
   * 关闭弹窗
   */
  closeMC:function(){
    var _this = this;
    _this.setData({
      ["auxiliary.showBasePop"]: false,
      ["auxiliary.showAnswerNo"]: false,
      ["auxiliary.showAnswerYes"]: false,
      ["auxiliary.showIssue"]: true,
      ["auxiliary.answerText"]: '',
    })
  }
})